import API from './index';

// 获取目的地列表（分页）
export const pageDestinations = (page = 1, size = 10, params = {}) => {
  return API.get('/destinations/page', {
    params: {
      page,
      size,
      ...params
    }
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return { records: [], total: 0 };
  });
};

// 获取热门目的地
export const getHotDestinations = (params) => {
  return API.get('/destinations/hot', { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 获取目的地详情
export const getDestinationDetail = (id) => {
  return API.get(`/destinations/${id}`).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return {};
  });
};

// 根据区域获取目的地
export const getDestinationsByRegion = (region, params = {}) => {
  return API.get(`/destinations/region/${region}`, { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 根据类型获取目的地
export const getDestinationsByType = (type, params = {}) => {
  return API.get(`/destinations/type/${type}`, { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};
//根据季节获取目的地
export const getDestinationsBySeason = (season, params = {}) => {
  return API.get(`/destinations/season/${season}`, { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};
// 增加目的地访问量
export const incrementDestinationVisits = (id) => {
  return API.post(`/destinations/${id}/increment-visits`);
};

// 获取目的地筛选选项
export const getDestinationFilterOptions = () => {
  return API.get('/destinations/filter-options').then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return {
      regions: [],
      types: [],
      seasons: [],
      themes: []
    };
  });
};

// 获取与目的地相关的路线
export const getRelatedRoutes = (destinationId, params = {}) => {
  return API.get(`/routes/related/${destinationId}`, { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  }).catch(error => {
    console.error("获取相关路线API错误:", error);
    return [];
  });
};